//////////////////////////////////////////////////////////////////////////

#ifndef _TIMERTEST_H_
#include "timertest.h"
#endif

//////////////////////////////////////////////////////////////////////////

#ifndef _TIMER_H_
#include "timer.h"
#endif

#include <iostream>
#include <cassert>

//////////////////////////////////////////////////////////////////////////

#if defined _WIN32
#include "windows.h"
#define PI_Sleep(msec) Sleep(msec);
#else
#include <unistd.h>
#define PI_Sleep(msec) usleep(msec);
#endif


/** Empty buffer test */
void TestNullTimer();


/** Simple timer test */
void TestSimpleTimer();


/** Blocking wait timer test */
void TestWaitingTimer();


//////////////////////////////////////////////////////////////////////////


void TestTimer()
{
    try
    {
        // Try several tests
        TestNullTimer();
        TestSimpleTimer();
        TestWaitingTimer();

        // If we are here -- all tests are OK
        std::cout << "Timer test OK!" << std::endl;
    }

    catch (const char* _str)
    {
        // Test failed -- print why
        std::cout << "Timer test failed" << std::endl
            << _str << std::endl;
    }
    catch (...)
    {
        // Test failed due to unknown reason
        std::cout << "Timer test failed" << std::endl;
    }
}


//////////////////////////////////////////////////////////////////////////


void TestNullTimer()
{
}


//////////////////////////////////////////////////////////////////////////


void TestSimpleTimer()
{
}


//////////////////////////////////////////////////////////////////////////


void TestWaitingTimer()
{
    Timer timer;
    timer.Start();
    size_t time1 = timer.Milliseconds();
    PI_Sleep(10);
    size_t time2 = timer.Milliseconds();

    // Some mistake possible
    if (time2 - time1 > 20 || time2 - time1 < 0)
        throw ("Timer wait for 10ms failed");

    timer.Reset();
    time1 = timer.Milliseconds();
    PI_Sleep(100);
    time2 = timer.Milliseconds();

    // Some mistake possible
    if (time2 - time1 > 110 || time2 - time1 < 90)
        throw ("Timer wait for 100ms failed");

    time1 = timer.Milliseconds();
    PI_Sleep(1000);
    time2 = timer.Milliseconds();

    // Some mistake possible
    if (time2 - time1 > 1020 || time2 - time1 < 980)
        throw ("Timer wait for 1000ms failed");
}


//////////////////////////////////////////////////////////////////////////
